package ch.fhnw.qualiscan.application.pages;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionModel;
import ch.fhnw.qualiscan.core.Page;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;

import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class EditQuestions extends Page implements ClickListener {
	
	public Domain currentDomain;
	public DomainModel domainModel;
	public QuestionModel questionModel;
	public TextField question;
	public TextField noSubQuestions;
	public TextField[] subQuestions;
	Button saveQuestion;
	Button saveSubQuestions;
	Question newQuestion;
	@Override
	public void buildLayout() {
		
		domainModel = new DomainModel();
		questionModel = new QuestionModel();
		
		
		if(currentDomain != null) {
			
			Label title = new Label(domainModel.getDomainText(currentDomain, userSession.getLanguage()).getText());
			HorizontalLayout questionLayout = new HorizontalLayout();
			question = new TextField();
			question.setRows(3);
			question.setColumns(20);
			noSubQuestions = new TextField();
			
			saveQuestion = new Button("Save");
			saveQuestion.setIcon(Config.ICON_SAVE);
			
			saveQuestion.addListener(this);
			
			questionLayout.addComponent(question);
			questionLayout.addComponent(noSubQuestions);
			
			this.addComponent(title);
			this.addComponent(questionLayout);
			this.addComponent(saveQuestion);
		}

	}
	
	public void buttonClick(ClickEvent event) {
		if (event.getButton() == saveQuestion) {
			if (noSubQuestions.getValue() != "") {
				this.removeAllComponents();
				subQuestions = new TextField[Integer.valueOf((String)noSubQuestions.getValue()).intValue()];
				for(int i = 0; i < subQuestions.length; i++) {
					subQuestions[i] = new TextField();
					subQuestions[i].setColumns(20);
					subQuestions[i].setRows(3);
					this.addComponent(subQuestions[i]);
				}
				saveSubQuestions = new Button("save");
				saveSubQuestions.addListener(this);
				this.addComponent(saveSubQuestions);
			}
			
			newQuestion = new Question();
			hibernateSession.getSession().save(newQuestion);
			Domain d = (Domain) hibernateSession.getSession().get(Domain.class, currentDomain.getId());
			d.addQuestion(newQuestion);
			hibernateSession.getSession().flush();
			hibernateSession.closeSession();
			
			QuestionText qt = new QuestionText();
			qt.setLanguage(userSession.getLanguage());
			qt.setText((String) question.getValue());
			hibernateSession.getSession().save(qt);
			hibernateSession.closeSession();
			
			newQuestion.addQuestiontext(qt);
			hibernateSession.getSession().update(newQuestion);
			hibernateSession.closeSession();
			
			
			question.setValue("");
			System.out.println("Question saved: "+newQuestion.getId()+"");
		}
		
		if(event.getButton() == saveSubQuestions) {
			
			for(int i = 0; i < subQuestions.length; i++) {
				Question sq = new Question();
				hibernateSession.getSession().save(sq);
				hibernateSession.closeSession();

				
				newQuestion.addSubquestion(sq);
				hibernateSession.getSession().update(newQuestion);
				hibernateSession.closeSession();

				QuestionText sqt = new QuestionText();
				sqt.setLanguage(userSession.getLanguage());
				sqt.setText((String) subQuestions[i].getValue());
				hibernateSession.getSession().save(sqt);
				hibernateSession.closeSession();
				

				sq.addQuestiontext(sqt);
				hibernateSession.getSession().update(sq);
				hibernateSession.closeSession();
				System.out.println("Subquestion added:"+sq.getId()+"");
				this.removeAllComponents();
				buildLayout();
			}
		}

	}

	@Override
	public void setNeededPermissions() {
		
	}

	@Override
	public void initComponents() {
		

	}

	@Override
	public void thirdParameterChanged(String thirdParameter) {
		int domainId;
		
		try {
			domainId = Integer.valueOf(thirdParameter).intValue();
		} catch(NumberFormatException ex) {
			NotificationLib.showError(TM.get("error"), TM.get("domain.error.exists"));
			return;
		}
		
		currentDomain = domainModel.getDomainById(domainId);
		if (currentDomain == null) {
			urlLib.setPage("Dashboard");
		}
		
		buildLayout();

	}

	@Override
	public void initModelsAndHelpers() {
		
		
	}

	

}
